package com.wlzg.collectiontool.view

import android.content.Context
import android.util.Log
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.lifecycle.lifecycleScope
import com.jaredrummler.materialspinner.MaterialSpinner
import com.lxj.xpopup.core.PositionPopupView
import com.wlzg.collectiontool.R
import com.wlzg.collectiontool.db.AppDatabase
import com.wlzg.collectiontool.model.bean.LoginRes
import com.wlzg.collectiontool.model.bean.PsXiangmu
import com.wlzg.collectiontool.util.DateUtil
import com.wlzg.collectiontool.util.MConfigUtil
import com.wlzg.collectiontool.util.QuyuPickerUtil
import com.wlzg.collectiontool.util.TimePickerUtil
import kotlinx.android.synthetic.main.new_pro.view.*
import kotlinx.android.synthetic.main.new_pro.view.diss
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.lang.Exception

class NewXmDia(var mContext: Context, var mFragment: ManageFragment,var psxm: PsXiangmu?,var dia:ProlistDia?=null) : PositionPopupView(mContext),
    View.OnClickListener, MaterialSpinner.OnItemSelectedListener<Any> {
    private var timeFlag = -1
    private var statusCode=0;
    private var proType=0;
    private var prolx=0;
    private var xmlb:String=""


    private var pvTimeU: TimePickerUtil? = null
    override fun getImplLayoutId(): Int {
        return R.layout.new_pro
    }

    override fun onCreate() {
        super.onCreate()
        pvTimeU = TimePickerUtil(mContext) {
            when (timeFlag) {
                0 -> startTime.text = it
                1 -> endTime.text = it
            }

        }
        leixing.setItems(MConfigUtil.sysAllDictItems?.pipe_survey as ArrayList)
        leixing.setOnItemSelectedListener { view, position, id, item ->
          prolx=position
        }
        status.setItems("进行中","完成")
        status.setOnItemSelectedListener { view, position, id, item ->
            status.setText(item as? String)
            statusCode=position
        }
        leixing.setItems(MConfigUtil.sysAllDictItems?.pipe_survey as ArrayList<LoginRes.SysAllDictItems.OlFormBizType>)
       leixing.setOnItemSelectedListener { view, position, id, item ->
             proType=position
           xmlb=item.toString()
       }
        zx.setText(mFragment.xmPoint?.x?.toString())
        zy.setText(mFragment.xmPoint?.y?.toString())
        startTime.setOnClickListener(this)
        endTime.setOnClickListener(this)
        diss.setOnClickListener(this)
        quxiao.setOnClickListener(this)
        shanchu.setOnClickListener(this)
        save.setOnClickListener(this)
        gzzongliang.inputType=EditorInfo.TYPE_CLASS_NUMBER

        psxm?.let {
            initData(it)
        }


    }


    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.startTime -> {
                timeFlag = 0
                pvTimeU?.pvTime?.show()
            }
            R.id.endTime -> {
                timeFlag = 1
                pvTimeU?.pvTime?.show()
            }
            R.id.diss -> {
                mFragment.ondelete(1,null)
                dismiss()
            }
            R.id.shanchu->{
                mFragment.ondelete(1,psxm)
                dismiss()
            }
            R.id.quxiao->{
                mFragment.ondelete(1,null)
                dismiss()
            }
            R.id.save -> {
                onSave()
                dismiss()
            }



        }
    }

    private fun onSave() {
        var xm:PsXiangmu=if(psxm==null){
            PsXiangmu()
        }else{
            psxm as PsXiangmu
        }
        xm.xmmc = xmName.text.toString()
        try {
            xm.totalWork=gzzongliang.text.toString().toInt()
        }catch (e:Exception){
            e.printStackTrace()
        }

        xm.xmcategory = xmlb
        xm.xmlb=proType
        xm.xmfzr = xmjingli.text.toString()
        xm.startTime = startTime.text.toString()
        xm.endTime = endTime.text.toString()
        xm.proStatus = statusCode.toString()
        xm.xmzt=statusCode
        xm.zoom=zoom.text.toString()
        xm.x1Coor = zx.text.toString()?.let {
            it.toDouble()
        }
        xm.y1Coor=zy.text.toString()?.let{
            it.toDouble()
        }
        xm.updateTime=DateUtil.getCurData()
        dia?.onUpdate()
        mFragment.lifecycleScope.launch(context = Dispatchers.IO) {
            if(psxm==null){
            val ids = AppDatabase.getInstance().userDao().insertPsXiangmu(xm)
            }else{
                AppDatabase.getInstance().userDao().updatePsXiangmu(xm)
            }
        }

    }
    private fun initData(xm:PsXiangmu){
        xmName.setText(xm.xmmc)
        leixing.selectedIndex=xm.xmlb
        gzzongliang.setText(xm.totalWork.toString())
        xmjingli.setText(xm.xmfzr)
        startTime.text = xm.startTime
        endTime.text=xm.endTime
        status.selectedIndex=xm.xmzt
        zoom.setText(xm.zoom)
        zx.setText(xm.x1Coor.toString())
        zy.setText(xm.y1Coor.toString())
    }

    override fun onItemSelected(view: MaterialSpinner?, position: Int, id: Long, item: Any?) {

    }


}